import { createRouter, createWebHistory } from 'vue-router'

let paths = localStorage.getItem('counter') ? JSON.parse(localStorage.getItem('counter') as string).auths : []

interface Routeritem {
  id?: number,
  path?: string,
  title?: string,
  component?: () => Promise<any>
  children?: Routeritem[]
}

const renderitem = (arr: any) => {
  let newarr: any = []
  arr && arr.forEach((item: any, index: number) => {
    if (item.childtype == 1) {
      newarr.push(
        {
          id: item.id,
          path: item.path,
          title: item.name,
          component: () => import('../views/home/' + item.component),
          children: renderitem(item.children)
        }
      )
    }
    else {
      newarr.push(
        {
          id: item.id,
          path: item.path,
          title: item.name,
          component: () => import('../views/home/' + item.component),
        }
      )
    }
  })
  return newarr
}

export const homeRouter = renderitem(paths)

// export const homeRouter = [
//   {
//     id: 1,
//     path: "/home/index",
//     name: '首页',
//     component: () => import('../views/home/Index.vue')
//   },
//   {
//     id: 2,
//     path: "/home/bigscreen",
//     name: "大屏管理",
//     component: () => import('../views/home/Bingscreen.vue'),
//     children: [
//       {
//         id: 3,
//         path: "/home/bigscreen/work",
//         name: "就业态势",
//         component: () => import('../views/home/Work.vue')
//       },
//       {
//         id: 4,
//         path: '/home/bigscreen/smart',
//         name: "智能检测",
//         component: () => import('../views/home/Smart.vue')
//       },
//       {
//         id: 5,
//         path: '/home/bigscreen/protection',
//         name: "安防管理",
//         component: () => import('../views/home/Protection.vue')
//       },
//       {
//         id: 6,
//         path: '/home/bigscreen/equipment',
//         name: "设备运维",
//         component: () => import('../views/home/Equipment.vue')
//       }
//     ]
//   },
//   {
//     id: 7,
//     path: "/home/knowledeg",
//     name: '知识点',
//     component: () => import('../views/Knowledge.vue')
//   },
//   {
//     id: 8,
//     path: "/home/page",
//     name: '页面管理',
//     component: () => import('../views/Page.vue')
//   },
//   {
//     id: 9,
//     path: "/home/tu",
//     name: '图形图表',
//     component: () => import('../views/Tu.vue')
//   }
// ]


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      redirect: '/login'
    },
    {
      path: "/login",
      name: '登录',
      component: () => import('../views/Login.vue')
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('../views/HomeView.vue'),
      children: homeRouter
    },
    {
      path: "/:pathMatch(.*)*",
      name: '*',
      component: () => import('../views/Error.vue')
    }
  ]
})
//路由守卫

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('counter') ? JSON.parse(localStorage.getItem('counter') as string).token : ''
  if (to.path !== '/login' && !token) {
    router.push('/login')
  }
  else {
    next()
  }
})


export default router
